package com.jeesite.modules.zx.echarts.web;


import com.google.common.collect.Lists;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.app.mockexam.theory.user.entity.TmockexamTheoryUser;
import com.jeesite.modules.app.mockexam.theory.user.service.TmockexamTheoryUserService;
import com.jeesite.modules.zx.echarts.entity.DataVo;
import com.jeesite.modules.zx.mockexam.theory.entity.TmockexamTheory;
import com.jeesite.modules.zx.mockexam.theory.service.TmockexamTheoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


@Controller
@RequestMapping(value = "${adminPath}/zx/echarts")
public class EchartsController {

    @Autowired
    private TmockexamTheoryService tmockexamTheoryService;
    @Autowired
    private TmockexamTheoryUserService tmockexamTheoryUserService;
     //TODO 对一套考试卷子 10次的考试结果的数据，进行分析


    @RequiresPermissions("zx:course:tcourses:view")
    @RequestMapping(value = {"index", ""})
    public String index(Model model) {
        TmockexamTheory entity = new TmockexamTheory();
        entity.setParentCode("0");
        model.addAttribute("theoryList",tmockexamTheoryService.findList(entity));
        return "modules/zx/echarts/index";
    }

    @GetMapping(value = "/createEcharts/{theoryId}")
    @ResponseBody
    public DataVo[]  createEcharts(@PathVariable(name = "theoryId") String theoryId,Model model) {

        TmockexamTheoryUser entity = new TmockexamTheoryUser();
        entity.setTheoryId(theoryId);
        entity.getSqlMap().getWhere().and("type", QueryType.IN, Lists.newArrayList(2,3));
        List<TmockexamTheoryUser> list = tmockexamTheoryUserService.findList(entity);
        List<TmockexamTheoryUser> bjglist =list.stream().filter(e->(e.getScore().compareTo(BigDecimal.ZERO)==1&&e.getScore().compareTo(new BigDecimal(60))==-1)).collect(Collectors.toList());
        List<TmockexamTheoryUser> lhlist =list.stream().filter(e->(e.getScore().compareTo(new BigDecimal(60))==1
                &&e.getScore().compareTo(new BigDecimal(80))==-1)).collect(Collectors.toList());
        List<TmockexamTheoryUser> yxlist =list.stream().filter(e->(e.getScore().compareTo(new BigDecimal(80))==1)).collect(Collectors.toList());

        DataVo dataVo = new DataVo("不及格",bjglist.size());
        DataVo dataVo1 = new DataVo("良好",lhlist.size());
        DataVo dataVo2 = new DataVo("优",yxlist.size());
        DataVo[] maps = {dataVo,dataVo1,dataVo2};
        return maps;
    }

}
